
/** 嵌套scrollView工具 
 * 注意:ScrollView、View、Content 锚点统一为 (0.5,1),
 * 注意:目前只支持了竖向滑动
 * UI层级为
 * parentScollView
 *  view
 *   content
 *    otheritem
 *    .
 *    .
 *    .
 *    childrenScorllView
 *     view
 *     content
 *      item
 *      .
 *      .
 *      .
 *          
 * 
 */
const { ccclass, property, menu } = cc._decorator;
@ccclass
@menu("自定义组件/ScrollView/嵌套ScrollView")
export default class NestScrollView extends cc.Component {
    _parent_scrollView: cc.ScrollView = null;
    _children_scrollView: cc.ScrollView = null;

    _oldY = cc.v2();
    /** 初始化父ScrollView和子ScroolView
     * 
     * @param p_sv 
     * @param c_sv 
     */
    init(p_sv: cc.ScrollView, c_sv: cc.ScrollView) {
        this._parent_scrollView = p_sv;
        this._children_scrollView = c_sv;
        this._onEvent();
    }
    release() {
        this._offEvent();
    }
    _onEvent() {
        this._children_scrollView.node.on(cc.Node.EventType.TOUCH_START, this._on_touch_start, this)
        this._children_scrollView.node.on(cc.Node.EventType.TOUCH_MOVE, this._on_touch_move, this)
    }
    _offEvent() {
        this._children_scrollView.node.off(cc.Node.EventType.TOUCH_START, this._on_touch_start, this)
        this._children_scrollView.node.off(cc.Node.EventType.TOUCH_MOVE, this._on_touch_move, this)
    }
    _on_touch_start(event: cc.Event.EventTouch) {
        this._oldY = event.getLocation()
        let py = this._parent_scrollView.content.y
        let cy = this._children_scrollView.content.y
        if ((this._checkCanUp(py)) || (this._checkCanDown(py, cy))) {
            this._setSwallow(false)
        } else {
            this._setSwallow(true)
        }
    }
    _on_touch_move(event: cc.Event.EventTouch) {
        // cc.log(event)
        let touch = event.getLocation()
        let y = touch.y
        let dy = y - this._oldY.y
        let py = this._parent_scrollView.content.y
        let cy = this._children_scrollView.content.y
        if ((dy > 0 && this._checkCanUp(py)) || (dy < 0 && this._checkCanDown(py, cy))) {
            this._movePV(dy)//移动父节点
            // cc.warn(touch.y, dy)
            this._setSwallow(false)
        } else {
            this._setSwallow(true)
        }
        this._oldY.y = y;
    }
    _setSwallow(flag) {
        if (this._children_scrollView.enabled != flag) {
            this._children_scrollView.enabled = flag;
        }
    }

    /** 滑动父SV */
    _movePV(dy) {
        let p = (this._parent_scrollView.content.y + dy) / (this._parent_scrollView.content.height - this._parent_scrollView.node.height)
        // cc.warn(p)
        this._parent_scrollView.scrollToOffset(cc.v2(this._parent_scrollView.content.x, this._parent_scrollView.content.y + dy),0.01)
    }
    /** 能否向下滑 */
    _checkCanDown(py, cy) {
        //已经滑动到了顶部 或者子SV还没滑到顶部
        if (py <= 0 || cy > 0) {
            return false
        }
        return true;
    }
    /** 能否向上滑 */
    _checkCanUp(y) {
        //已经滑动到了底部
        if (y >= (this._parent_scrollView.content.height - this._parent_scrollView.node.height - 1)) {
            return false
        }
        return true;
    }
}
